package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.shopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 先根据用户id获取对应商品, 是套餐还是菜品
        Long UserID = BaseContext.getCurrentId();
        shoppingCart.setUserId(UserID);
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);

        // 获取对应的数量，减1处理，如果是1应该将该菜品删除
        if(list != null && !list.isEmpty()){
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();


            if(number ==1){
                // 直接根据主键id进行删除
                shopCartMapper.deteById(cart.getId());
            }else{
                cart.setNumber(number-1);
                shopCartMapper.update(cart);
            }

        }
    }



    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shopCartMapper.deleteByUserId(userId);
    }

    @Override
    public List<ShoppingCart> getList() {

        Long currentId = BaseContext.getCurrentId();

        ShoppingCart cart = ShoppingCart.builder().userId(currentId).build();

        return shopCartMapper.list(cart);
    }

    @Autowired
    private shopCartMapper shopCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {

        // 判断当前加入到购物车的商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);


        // 如果已经存在，执行数量加操作
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shopCartMapper.update(cart);
        }else { // 不存在插入表中
            //判断本次添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null) {
                Dish dish = dishMapper.getByIds(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            }else {
                Setmeal setmeal = new Setmeal();
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopCartMapper.save(shoppingCart);
        }
    }
}
